package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.BannerQueryObject;
import cn.wolfcode.wolf2w.repository.BannerMongoRepository;
import cn.wolfcode.wolf2w.service.IBannerService;
import cn.wolfcode.wolf2w.service.ITravelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class BannerServiceImpl implements IBannerService {

    @Autowired
    private BannerMongoRepository repository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private ITravelService travelService;

    @Override
    public void save(Banner banner) {
        repository.save(banner);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public void update(Banner banner) {
        repository.save(banner);
    }

    @Override
    public Banner find(String id) {
        Banner banner = repository.findById(id).orElse(null);

        return banner;
    }

    @Override
    public List<Banner> findAll() {
        List<Banner> list = repository.findAll();
        return list;
    }

    @Override
    public Page query(BannerQueryObject qo) {
        Query query = new Query();
        long count = template.count(query, Banner.class);
        if(count == 0){
            return Page.empty();
        }
        Pageable page = PageRequest.of(qo.getCurrentPage() - 1,qo.getPageSize(),Sort.Direction.ASC,"_id");
        query.with(page);
        List<Banner> list = template.find(query, Banner.class);

        return new PageImpl(list,page,count);
    }

    @Override
    public List<Banner> queryTravelTop5() {
        List<Banner> list = repository.findByTypeAndState(Banner.TYPE_TRAVEL,Banner.STATE_NORMAL,PageRequest.of(0,5,Sort.Direction.ASC,"sequence"));
        return list;
    }

    @Override
    public Banner queryStrategyTop1() {
        List<Banner> list = repository.findByTypeAndState(Banner.TYPE_STRATEGY,Banner.STATE_NORMAL,PageRequest.of(0,5,Sort.Direction.ASC,"sequence"));
        return list.get(0);
    }
}
